package com.example.testgame.sprite;

import java.util.HashMap;
import java.util.Map;

import com.example.testgame.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MapBlock {
	
	public BlockType type;
	
	public MapBlock(BlockType type) {
		this.type = type;
	}
	
	
	public enum BlockType {
		ground, tree, hill, water
	}
	
	/**
	 * 将地图int数据转换为地图数据
	 * @param mapDataInt
	 * @return
	 */
	public static BlockType[][] transMapData(int[][] mapDataInt) {
		if(mapDataInt != null && mapDataInt.length > 0 && mapDataInt[0].length > 0) {
			BlockType[][] mapData = new BlockType[mapDataInt.length][mapDataInt[0].length];
			for (int i = 0; i < mapData.length; i++) {
				for (int j = 0; j < mapData[i].length; j++) {
					mapData[i][j] = BlockType.values()[mapDataInt[i][j]];
				}
			}
			return mapData;
		}
		return null;
	}
	
	
	private static Map<BlockType, Bitmap> images = new HashMap<MapBlock.BlockType, Bitmap>();
	
	/**
	 * 根据地图类型获取绘制图片
	 * @param ctx
	 * @param type
	 * @return
	 */
	public static Bitmap getImage(Context ctx, BlockType type) {
		Bitmap b = images.get(type);
		if(b != null && !b.isRecycled()) {
			return b;
		}
		int rid;
		switch (type) {
		case ground:
			rid = R.drawable.map_ground;
			break;
		case tree:
			rid = R.drawable.map_tree;
			break;
		case hill:
			rid = R.drawable.map_hill;
			break;
		case water:
			rid = R.drawable.map_water;
			break;
		default:
			rid = R.drawable.map_ground;
			break;
		}
		b = BitmapFactory.decodeResource(ctx.getResources(), rid);
		images.put(type, b);
		return b;
	}

}
